package com.sky.task;

import com.sky.entity.Orders;
import com.sky.mapper.OrdersMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author hyh
 * @version 1.0
 */
@SuppressWarnings({"all"})
@Slf4j
@Component
public class OrderTask {

    @Autowired
    OrdersMapper ordersMapper;

    @Scheduled(cron = "0 * * * * ?")//m f h d y z n
    public void processOutTime() {
        log.info("定时处理超时订单{}", LocalDateTime.now());
        //查询超时的订单
        LocalDateTime time = LocalDateTime.now().plusMinutes(-15);

        List<Orders> orders = ordersMapper.selectOutTime(Orders.PENDING_PAYMENT, time);
        //对超时的订单进行处理
        if (orders != null && orders.size() > 0) {
            for (Orders order : orders) {
                order.setStatus(Orders.CANCELLED);
                order.setCancelTime(LocalDateTime.now());
                order.setCancelReason("订单超时，自动取消");
                ordersMapper.update(order);
            }
        }
    }
    @Scheduled(cron = "0 0 1 * * ?")//m f h d y z n
    public void processDeliveryOrder(){
        log.info("定时处理未结算的已经派送的订单");
        LocalDateTime time = LocalDateTime.now().plusMinutes(-60);
        List<Orders> orders = ordersMapper.selectOutTime(Orders.DELIVERY_IN_PROGRESS,time);
        //对定时处理未结算的已经派送的订单进行处理
        if (orders != null && orders.size() > 0) {
            for (Orders order : orders) {
                order.setStatus(Orders.COMPLETED);
                ordersMapper.update(order);
            }
        }
    }



}
